// 这个文件专门用来存放关于首页的所有接口
const Router = require('koa-router');
const category = new Router();
const db = require('../utils/db');

// 写对应的接口
category.get('/', async (ctx) => {
    let mydata=await new Promise((resolve, reject) => {
        // 访问数据库，拿到banner这个数据给前端
        return db.query(`select * from category`, (err, data) => {
            if (err) throw err;
            console.log(data);
            data.map(val=>{
               val.sortimage='http://localhost:5050'+val.sortimage;
            })
            resolve(data);
        });
    })
    ctx.body = mydata;
})

category.get('/1', async (ctx) => {
    let mydata=await new Promise((resolve, reject) => {
        // 访问数据库，拿到banner这个数据给前端
        return db.query(`select * from goods where sortid=1`, (err, data) => {
            if (err) throw err;
            console.log(data);
            data.map(val=>{
               val.goodsimg='http://localhost:5050'+val.goodsimg;
            })
            resolve(data);
        });
    })
    ctx.body = mydata;
})

category.get('/2', async (ctx) => {
    let mydata=await new Promise((resolve, reject) => {
        // 访问数据库，拿到banner这个数据给前端
        return db.query(`select * from goods where sortid=2`, (err, data) => {
            if (err) throw err;
            console.log(data);
            data.map(val=>{
               val.goodsimg='http://localhost:5050'+val.goodsimg;
            })
            resolve(data);
        });
    })
    ctx.body = mydata;
})
category.get('/3', async (ctx) => {
    let mydata=await new Promise((resolve, reject) => {
        // 访问数据库，拿到banner这个数据给前端
        return db.query(`select * from goods where sortid=3`, (err, data) => {
            if (err) throw err;
            console.log(data);
            data.map(val=>{
               val.goodsimg='http://localhost:5050'+val.goodsimg;
            })
            resolve(data);
        });
    })
    ctx.body = mydata;
})
category.get('/4', async (ctx) => {
    let mydata=await new Promise((resolve, reject) => {
        // 访问数据库，拿到banner这个数据给前端
        return db.query(`select * from goods where sortid=4`, (err, data) => {
            if (err) throw err;
            console.log(data);
            data.map(val=>{
               val.goodsimg='http://localhost:5050'+val.goodsimg;
            })
            resolve(data);
        });
    })
    ctx.body = mydata;
})
category.get('/5', async (ctx) => {
    let mydata=await new Promise((resolve, reject) => {
        // 访问数据库，拿到banner这个数据给前端
        return db.query(`select * from goods where sortid=5`, (err, data) => {
            if (err) throw err;
            console.log(data);
            data.map(val=>{
               val.goodsimg='http://localhost:5050'+val.goodsimg;
            })
            resolve(data);
        });
    })
    ctx.body = mydata;
})

module.exports = category;